#ifndef _NETCAP_H
#define _NETCAP_H
#include <string>
#include <pcap.h>
#include <map>
#include <vector>
#include <iostream>
#ifdef WIN32
#include <stdint.h>
#else
#include <arpa/inet.h>
#include <pthread.h>
#endif

#include "event.h"
using namespace std;

#define PACKETTHRESHOLD 2000




typedef struct _netcardinfo{
	char name[256];
	char desc[256];
	unsigned char mac[6];
	unsigned long ip;
	unsigned long mask;
}netcardinfo;

typedef struct _recvframedescri
{
    vector<unsigned char> data;
    struct pcap_pkthdr pkth;
    
} recvframedescri ;




typedef struct _ipinfo
{
    char ip[20];
    unsigned int  times;
} ipinfo ;

typedef struct  macinfo
{
    unsigned int  newframe;
    unsigned int  oldframe;
    struct in_addr ip ;
} macinfo ;

class  NetCap
{
public:
	NetCap(string filtRule);
	~NetCap( );

	static void * ping(void* param);
	static void * ipdetect(void* param);
	void CreateCap(bool netMode,string filtRule);
	void SetFilter(string filtRule);
	void CapDeal(unsigned int  *user, const  struct pcap_pkthdr *pkthdr, const u_char *packet);

	void  updata_info();
	unsigned int GetByteSize();
	void GetByteSize(unsigned int totals);
	int getMac(const char *iface, int *ifindex, unsigned int *addr, unsigned char *mac);
	void getNetCardList(std::map<std::string,netcardinfo>& v );
	void findmac(char *dev , unsigned char *mac );
	unsigned int getnetflag();
	string getFilterRule();
	uint64_t getNetCradFlow();
	uint64_t getLocalNetUpFlow();
	uint64_t getLocalNetDownFlow();
	string   getmacs(std::vector<pair<uint64_t,macinfo> > &macs);
	void arpx( unsigned int host, unsigned int mask  ,int size);
	void clearip();	
	void  clearMacs();
	void getdetect(std::map<std::string ,uint64_t>&ips);
	void  set_netstorm_threshold(unsigned int  threshold);
   	unsigned int setDetectArg(in_addr arg_ip ,unsigned long  arg_mask);
	unsigned int setDetectArg(in_addr arg_ip ,unsigned int  arg_size ,int flag);
	void MacToInfo(const char* fileroad ,std::map< std::string,std::string>&macRuleAddr);
	std::map<std::string, netcardinfo> m_NetCardList;

	std::map<uint64_t,macinfo> maclist; 

	std::map<std::string ,uint64_t > ipdetectinfo;
	in_addr ip;
	in_addr detectip;
	unsigned long  mask;
	unsigned int detectsize;
	std::map< std::string,std::string> *mac2info;
#ifdef WIN32
	int threadid; 
	int threadid1;
#else		
	pthread_t  threadid;
	pthread_t  threadid1;
#endif
	unsigned int flag;
	char * devName ;
	event_handle ipevent;
	event_handle macevent;
	event_handle event;
	bool ipdetectcontrl;
private:
	unsigned char uploadhost[6];
	pcap_t * _pcap; 
	char errBuf[PCAP_ERRBUF_SIZE];
		
	string filterRule;
	struct bpf_program filter;
	long unsigned int totals;
	long unsigned int uptotals;
	long unsigned int downtotals;
	long unsigned int filtertotals;
	unsigned int  curpacketnum;
	unsigned int oldpacketnum ;
	unsigned int  net_storm_threshold ;
	unsigned int  packet_per_second;

	struct pcap_pkthdr packet_descri;

     
};


#endif
